package com.zz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zz.common.R;
import com.zz.entity.SystemLog;
import com.zz.mapper.SystemLogMapper;
import com.zz.service.SystemLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Nullable;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;


/**
 * 系统日志Service实现
 *
 * @author 朱彦燕
 *
 */
@Service
@Validated
public class SystemLogServiceImpl extends ServiceImpl<SystemLogMapper, SystemLog> implements SystemLogService {

    @Autowired
    private SystemLogMapper systemLogMapper;

    @Override
    public R<Page> sLog(int page, int pageSize, @Nullable String startTime, @Nullable String endTime) {
        // 条件过滤器
        LambdaQueryWrapper<SystemLog> wrapper = new LambdaQueryWrapper<>();
        // 按创建时间降序排序
        wrapper.orderByDesc(Boolean.parseBoolean("create_date"));

        // 时间转化格式
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 将字符串转为时间
        if (!StringUtils.isEmpty(startTime)){
            LocalDateTime sTime = LocalDateTime.parse(startTime, dateTimeFormatter);
            // 大于等于
            wrapper.ge(SystemLog::getCreateDate, sTime);
        }
        if (!StringUtils.isEmpty(endTime)){
            LocalDateTime eTime = LocalDateTime.parse(endTime, dateTimeFormatter);
            // 小于等于
            wrapper.le(SystemLog::getCreateDate, eTime);
        }

        Page<SystemLog> pageInfo = new Page<>(page, pageSize);
        Page<SystemLog> pages = systemLogMapper.selectPage(pageInfo, wrapper);

        return R.success(pages);
    }
}
